
#!/bin/sh
#set -x
environment="test"
dir="/sunlands/workspace/"
parentfir="desk_mate_deliver/"
logsdir="/sunlands/logs/"
appsdir="/sunlands/apps/"
applicationName="desk-mate-push"
packageFlag=1
declare -A modelType

modelType[desk-mate-push]=web;

function mergeCode {
        echo "mergeCode";
        cd ${dir}${parentfir};
        pwd
        git merge origin/${branchName};
        cd ..;
}

function updateCode {
        echo "更新代码"
        cd ${dir}${parentfir}
        git fetch
        cd ..
}
function innerPackage {
        echo "编译发布包"
        cd ${dir}${parentfir}
        mvn clean install -Dmaven.test.skip=true -U
        cd ..
}

function package {
         updateCode;
         mergeCode;
         innerPackage;
}


function backWeb {
         rm -f  /sunlands/apps/${modelName}.jar
        cp ${dir}${parentfir}${applicationName}/target/${modelName}.jar /sunlands/apps/
        #mv ${appsdir}${modelName}.jar ${appsdir}backup/${modelName}.jar_`date +%Y%m%d%H%M`
}


function stopWeb {
	echo "关闭服务 PORT=$1"
       echo "curl -sS -XPUT ${eureka}/apps/${applicationName}/${selfhost}:${applicationName}:$1/status?value=OUT_OF_SERVICE" 
 	  HTTP_CODE=`curl -sS -XPUT ${eureka}/apps/${applicationName}/${selfhost}:${applicationName}:$1/status?value=OUT_OF_SERVICE` 
	    echo "下线服务:${HTTP_CODE}"
SERVER_PID=`ps -ef | grep "Dserver.port=$1" | grep -v "grep" | awk '{print $2}'` 

            echo "server pid is ${SERVER_PID}"
            if [ -n "$SERVER_PID" ]; then
 echo "下线旧的微服务，等待70秒，让eureka服务缓存拉取到最新的数据"
sleep 70;
                    kill -9 ${SERVER_PID}
                    echo "${SERVER_PID} is killed!"
		    HTTP_CODE=`curl -sS -XDELETE ${eureka}/apps/${applicationName}/${selfhost}:${applicationName}:$1` 
            fi
            

        
        
}

function startWeb {



 if (( $1 == ${closePort} )); then
               echo "启动备份服务"
                 nohup java -Dspring.profiles.active=${environment} -Dserver.port=$1  -Dlogging.path=/sunlands/logs/backup -Dlogging.file=${applicationName}-beifen.log  -server -Xms128m -Xmx256m -jar /sunlands/apps/${applicationName}.jar  >> /sunlands/logs/${applicationName}-${closePort}.log 2>&1 &
            else
                nohup java -Dspring.profiles.active=${environment} -Dserver.port=$1 -server -Xms128m -Xmx256m -jar /sunlands/apps/${applicationName}.jar  >> /sunlands/logs/${applicationName}.log 2>&1 &
            fi



for (( i= 0; i < 70; i++))
   do  
        HTTP_CODE=`curl -o /dev/null --retry 3 --retry-max-time 8 -s -w "%{http_code}" "http://127.0.0.1:$1/info"` 
        if [ ${HTTP_CODE} == 200 ]; then
            echo "服务启动成功！！！！！！port = $1";
	    echo "等待5秒，让eureka服务缓存拉取到最新的数据！";
            sleep 5;
            break;
        else
            if (( i > 60 )); then
                echo "fail 服务启动超过60秒，启动失败，请检查程序！port = $1"
                exit 1;
            else
                echo "服务正在启动，耗时：$i";
                sleep 1;
            fi
        fi
   done
}

function deployWeb {
        backWeb;
        for port in "${ports[@]}";
        do
            echo "开始部署服务端口为：$port >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
            stopWeb $port;
            startWeb $port;
            echo "$port"               
        done  
        echo "关闭之前启动的备份服务，端口${closePort}"
        stopWeb ${closePort};
}

function deploy {

        if [ "${modelType[${modelName}]}" == "web" ]; then
                deployWeb;
        else
                echo "模块名${modelName}没有注册"
                exit 0
        fi
}


if [ "$1" = "-c" ]; then
  packageFlag=1
  shift
fi

modelName=${applicationName};
branchName="test";
ports=("8383" "8384")   #通过启动替代服务的方案，防止服务终端，实现无感知升级
closePort=${ports[0]};  #启动完毕后关闭替代服务
selfhost=172.16.0.13;

eureka=http://sunlands:HL9i8gWoHuKLmEr9@172.16.0.7:8761/eureka;
echo "你正在在 ${environment} 环境部署应用${dir}${applicationName}模块,分支名称${branchName}"
if [ "${packageFlag}" == "1" ]; then
        package

fi
if [ "${modelName}V" != "V" ]; then
 deploy ${modelName}
fi
